package com.ccod.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccod.goods.mapper.GoodsMapper;
import com.ccod.goods.model.GoodsDO;
import com.ccod.nacos.goods.facade.GoodsDto;
import com.ccod.nacos.goods.model.GoodsFacade;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: ccod
 * @data: 2020/12/27 13:25
 **/
@DubboService
public class GoodsFacadeImpl implements GoodsFacade {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<GoodsDto> searchGoods() {
        List<GoodsDO> goodsDOS = goodsMapper.selectList(new QueryWrapper<GoodsDO>());
        List<GoodsDto> result = Lists.newArrayList();
        goodsDOS.forEach(item->{
            GoodsDto goodsDto = new GoodsDto();
            BeanUtils.copyProperties(item,goodsDOS);
            result.add(goodsDto);
        });
        return result;
    }

    @Override
    public boolean addGoods(GoodsDto dto) {
        if(dto == null){
            System.out.println("新增商品非法参数");
            return false;
        }
        GoodsDO goodsDO = new GoodsDO();
        BeanUtils.copyProperties(dto,goodsDO);
        return goodsMapper.insert(goodsDO) > 0;
    }
}
